﻿namespace Smart.LiveUpdate
{
    using Smart.Utils.Net;
    using System;
    using System.IO;
    using System.Windows.Forms;

    public class MyAutoUpdate
    {
        private static string _Newversion;
        private static string _Oldversion;
        private static string _url;

        public static bool Check()
        {
            new DownLoadHelper(URL).Start();
            FileInfo info = new FileInfo(Application.StartupPath + @"\Version.txt");
            if (info.Exists)
            {
                StreamReader reader = new StreamReader(Application.StartupPath + @"\Version.txt");
                try
                {
                    try
                    {
                        string str;
                        if (((str = reader.ReadLine()) != "0") && str.StartsWith("Version:"))
                        {
                            Newversion = str.Replace("Version:", "").Trim();
                            if (string.Compare(Newversion, Oldversion, true) > 0)
                            {
                                reader.Close();
                                return true;
                            }
                        }
                    }
                    catch
                    {
                    }
                }
                finally
                {
                }
                reader.Close();
            }
            return false;
        }

        public static void CheckVersion()
        {
            Check();
        }

        public static void CheckVersion(string url, string version)
        {
            URL = url;
            Oldversion = version;
            if (Internet.IsConnectedToInternet())
            {
                Check();
            }
        }

        public static string Newversion
        {
            get
            {
                return _Newversion;
            }
            set
            {
                _Newversion = value;
            }
        }

        public static string Oldversion
        {
            get
            {
                return _Oldversion;
            }
            set
            {
                _Oldversion = value;
            }
        }

        public static string URL
        {
            get
            {
                return _url;
            }
            set
            {
                _url = value;
            }
        }
    }
}

